package com.gzw.scm.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.gzw.scm.R;
import com.gzw.scm.bean.Info;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 一键出库EecyclerView适配器
 * @author: Qiu Guoxing
 * @date: 2018/2/27 下午 2:28
 * @update: 2018/2/27
 * @version: 1.0
 */

public class YJCKAdapter extends RecyclerView.Adapter<YJCKAdapter.ViewHolder> {

    private Context context;
    private List<Info> list;
    private List<Boolean> mSelectedPosition;
//    private boolean mIsSelectable = false;

    public YJCKAdapter(Context context, List<Info> list) {
        this.context = context;
        this.list = list;
        mSelectedPosition = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            mSelectedPosition.add(i,true);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_yjck, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, @SuppressLint("RecyclerView") final int position) {

//        holder.select.setChecked(true);
        holder.select.setChecked(isItemChecked(position));
        holder.xu.setText(String.valueOf(position+1));
        holder.number.setText(list.get(position).getTboxno());
        holder.label.setText(list.get(position).getOrdnoid1());
        holder.order.setText(list.get(position).getWordernumber());
        holder.supplier.setText(list.get(position).getSuppname());

        holder.select.setOnClickListener(v -> {
                if (mSelectedPosition.get(position)) {
                    setItemChecked(position, false);
                } else {
                    setItemChecked(position, true);
                }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void add(Info info) {
        mSelectedPosition.add(true);
        list.add(info);
        notifyDataSetChanged();
    }

    private boolean isItemChecked(int position) {
        return mSelectedPosition.get(position);
    }

    private void setItemChecked(int position, boolean isChecked) {
        mSelectedPosition.set(position, isChecked);
    }

    public List<Info> getList() {
        ArrayList<Info> allItem = new ArrayList<>();
        allItem.addAll(list);
        return allItem;
    }

    public void clear() {
        list.clear();
        mSelectedPosition.clear();
        notifyDataSetChanged();
    }

    public List<Info> getCheckedList() {
        ArrayList<Info> allItem = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            if(isItemChecked(i)){
                allItem.add(list.get(i));
            }
        }
        return allItem;
    }

    /**
     * 获取查询数据的position之后RecyclerView滑动到此处
     *
     * @param box 查询的周转箱编号
     * @return 位置
     */
    public int getPosition(String box) {
        ArrayList<String> boxList = new ArrayList<>();
        for (Info info : list) {
            boxList.add(info.getTboxno());
        }
        if (boxList.contains(box))
            return boxList.indexOf(box);
        else
            return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private LinearLayout layout;
        private CheckBox select;
        private TextView number;
        private TextView label;
        private TextView order;
        private TextView supplier;
        private TextView xu;

        ViewHolder(View itemView) {
            super(itemView);
            layout = ((LinearLayout) itemView.findViewById(R.id.ll_layout));
            select = ((CheckBox) itemView.findViewById(R.id.cb_select));
            number = ((TextView) itemView.findViewById(R.id.number));
            label = ((TextView) itemView.findViewById(R.id.label));
            order = ((TextView) itemView.findViewById(R.id.order));
            supplier = ((TextView) itemView.findViewById(R.id.supplier));
            xu = ((TextView) itemView.findViewById(R.id.xu));
        }
    }
}
